﻿#include "coladdrecorddialog.h"
#include "ui_coladdrecorddialog.h"
#include <QSqlQuery>
int COLaddrecordDialog::AID;
COLaddrecordDialog::COLaddrecordDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::COLaddrecordDialog)
{
    ui->setupUi(this);
    ui->frame->resize(this->geometry().width(), ui->frame->height());
    ui->status->setText("添加信息状态：");
    ui->lenddate->setDate(QDate::currentDate());
    ui->returndate->setDate(QDate::currentDate());
    connect(ui->cancelbtn,SIGNAL(clicked(bool)),this,SLOT(close()));
}

COLaddrecordDialog::~COLaddrecordDialog()
{
    delete ui;}

void COLaddrecordDialog::receiveApprovalID(int ID)
{AID=ID;}

void COLaddrecordDialog::on_ackbtn_clicked()
{
    if(ui->object->text().isEmpty()||ui->readman->text().isEmpty()||ui->readunit->text().isEmpty()||ui->objectunit->text().isEmpty()||ui->content->text().isEmpty()){
        ui->status->setText("添加信息状态:请至少添入阅档日期,阅档人及单位，阅档对象及单位，阅档内容！");
   }
    else {
    QSqlQuery q;
    q.prepare("INSERT INTO recordall(recordID,lenddate,returndate,lendman,lendunit,object,objectunit,content,others,ApprovalID) "
              "VALUES(:recordID,:lenddate,:returndate,:lendman,:lendunit,:object,:objectunit,:content,:others,:ApprovalID)");
    q.bindValue(":lenddate",ui->lenddate->text());
    if(ui->checkBox->isChecked()){
           q.bindValue(":returndate",ui->returndate->text());
    }
    q.bindValue(":lendman",ui->readman->text());
    q.bindValue(":lendunit",ui->readunit->text());
    q.bindValue(":object",ui->object->text());
    q.bindValue(":objectunit",ui->objectunit->text());
    q.bindValue(":content",ui->content->text());
    q.bindValue(":others",ui->others->text());
    q.bindValue(":ApprovalID",AID);
    bool ok=q.exec();
    if(ok)
    {ui->status->setText("添加信息状态:添加成功");
            this->close();}
    else
    {ui->status->setText("添加信息状态:添加失败");}
}
}

void COLaddrecordDialog::on_checkBox_clicked()
{
    if(ui->checkBox->isChecked()){
        ui->returndate->setEnabled(true);
    }else{ui->returndate->setEnabled(false);}
}
